/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.importpremium;

import com.kmcsoft.goldshop.generic.service.ShopGenericService;
import com.kmcsoft.goldshop.importpremium.service.ImportPremiumService;
import com.kmcsoft.goldshop.model.master.ProductPremiumJewelry;
import com.kmcsoft.goldshop.model.scm.ImportJewelryDetail;
import com.kmcsoft.goldshop.model.scm.ImportJewerlyOrder;
import java.text.NumberFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;

/**
 *
 * @author huyvu
 */
public class OrderInformation extends GenericForwardComposer {

    @Autowired
    private ImportPremiumService importPremiumService;
    @Autowired
    private ShopGenericService shopGenericService;
    private double totalWeight;
    private double totalCost;
    private double totalCostInBasicCurrency;
    private String code;
    private double totalAmount;
    private double totalSellingPrice;
    private NumberFormat numberFormat;
    private AnnotateDataBinder binder;
    private String unit;
    private String costCurrency;
    private String basicCurrency;
    private String style;
    private boolean group;
    

    public String getCode() {
        return code;
    }

    public String getStyle() {
        return style;
    }

    public String getTotalAmount() {
        return numberFormat.format(totalAmount);
    }

    public String getTotalCost() {
        return numberFormat.format(totalCost);
    }

    public String getTotalCostInBasicCurrency() {
        return numberFormat.format(totalCostInBasicCurrency);
    }
    
    public String getTotalWeight() {
        return numberFormat.format(totalWeight);
    }

    public String getTotalSellingPrice() {
        return numberFormat.format(totalSellingPrice);
    }

    public String getBasicCurrency() {
        return basicCurrency;
    }

    public String getCostCurrency() {
        return costCurrency;
    }

    public String getUnit() {
        return unit;
    }

    public boolean isSpecialGroup() {
        return group;
    }
    public boolean isNormalGroup() {
        return !group;
    }
    

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        numberFormat = NumberFormat.getInstance();
        self.setAttribute("modal", this);
        ImportJewerlyOrder order = shopGenericService.getImportJewerlyOrderById_DetachDetail(((Long) arg.get("importOrderId")));
        code = "" + order.getCode();
        totalAmount = order.getDetails().size();

        totalWeight = 0;
        totalCost = 0;
        totalCostInBasicCurrency = 0;
        totalSellingPrice = 0;
        boolean first = true;
        for (ImportJewelryDetail detail : order.getDetails()) {
            ProductPremiumJewelry premiumJewelry = importPremiumService.getProductPremiumJewelryById(detail.getProduct().getId());
            if (first) {
                unit = premiumJewelry.getUnit().getName();
                basicCurrency = premiumJewelry.getCurrency().getCode();
                costCurrency = premiumJewelry.getCostCurrency().getCode();
                style=premiumJewelry.getStyle().getName();
                group=premiumJewelry.getJewelryType().isSpecialGroup();
                first = false;
            }
            totalWeight += premiumJewelry.getJewelryWeight();
            totalCost += premiumJewelry.getCost();
            totalCostInBasicCurrency += premiumJewelry.getCostInBasicCurrency();
            totalSellingPrice += premiumJewelry.getSellingPrice();
        }
        binder = new AnnotateDataBinder(self);
        self.setAttribute("binder", binder);
        binder.loadAll();
    }

    public void onDetach() {
        self.detach();
    }
}
